package com.andatsoft.laisim.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.andatsoft.laisim.R;
import com.andatsoft.laisim.entity.Song;
import com.andatsoft.laisim.entity.TwoLineItem;
import com.andatsoft.laisim.theme.Theme;
import com.andatsoft.laisim.theme.ThemeManager;

public class LibraryTwoLineListAdapter extends ArrayAdapter<Song> {

	private List<TwoLineItem> mItemList;
	private int mSelectedItem = -1;
	private LayoutInflater mInflater;
	private Context mContext;
	private Theme mTheme;

	public LibraryTwoLineListAdapter(Context context, List<TwoLineItem> items) {
		super(context, 0);
		this.mContext = context;
		this.mItemList = items;
		this.mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mTheme = ThemeManager.getSelectedTheme();
	}

	public class ViewHolder {
		public TextView tvName;
		public TextView tvCount;
	}

	@Override
	public int getCount() {
		if (mItemList == null)
			return 0;
		return mItemList.size();
	}

	@SuppressLint("NewApi") @Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		ViewHolder vHolder;
		if (view == null) {
			view = mInflater.inflate(R.layout.library_artist_item, parent,
					false);
			vHolder = new ViewHolder();
			TextView tvName = (TextView) view
					.findViewById(R.id.tv_library_artist_name);
			vHolder.tvName = tvName;
			TextView tvCount = (TextView) view
					.findViewById(R.id.tv_library_artist_song_count);
			vHolder.tvCount = tvCount;
			view.setTag(vHolder);
		} else {
			vHolder = (ViewHolder) view.getTag();
		}
		// Because the view holder is holding the reference of the text
		// view...
		vHolder.tvName.setText(mItemList.get(position).name);
		int count = mItemList.get(position).count;
		if (count <= 1) {
			vHolder.tvCount.setText(count + " "
					+ mContext.getString(R.string.lib_name_song));
		} else {
			vHolder.tvCount.setText(count + " "
					+ mContext.getString(R.string.lib_name_song_plural));
		}
		// // highlight view selected
		if (position == this.mSelectedItem) {
			
			vHolder.tvName.setTextSize(mTheme.getMPLSelectedTitleSize());
			vHolder.tvName.setTextColor(mTheme.getMPLSelectedTitleColor());
			vHolder.tvCount.setTextColor(mTheme.getMPLSelectedArtistColor());
			vHolder.tvCount.setTextSize(mTheme.getMPLSelectedArtistSize());
		} else {
			
			vHolder.tvName.setTextSize(mTheme.getMPLNormalTitleSize());
			vHolder.tvName.setTextColor(mTheme.getMPLNormalTitleColor());
			vHolder.tvCount.setTextColor(mTheme.getMPLNormalArtistColor());
			vHolder.tvCount.setTextSize(mTheme.getMPLNormalArtistSize());
		}
		return view;
	}

	public void setData(List<TwoLineItem> items) {
		this.mItemList = items;
		notifyDataSetChanged();
	}

	public void setSelectedItem(int pos) {
		if (pos > mItemList.size() - 1 || pos < 0)
			pos = -1;
		this.mSelectedItem = pos;
	}
}
